<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // dd($request -> all());
        //参考的sql语句
        // SELECT g1.*,CONCAT(c1.path,',',c1.id) as path_str,c1.tname as shopname FROM t_goods as g1 RIGHT JOIN t_class as c1 on g1.type_id = c1.id ORDER BY path_str;
        $data = DB::table('t_class as c1') ->select('*',DB::raw("concat(c1.path,',',c1.id) as s_path"))
         ->orderBy('s_path') -> where('tname', 'like', '%'.$request -> keywords.'%') -> paginate($request -> input('num', 10));
        // dd($data -> all());


        // $data = DB::table('t_class') -> select('*',DB::raw("concat(path,',',id) as path_str")) -> orderBy('path_str') -> get();
        // foreach ($data as $k => $v) {
        //     $num = substr_count($v -> path, ',');
        //     $str = str_repeat('----|', $num);
        //     $data[$k] -> tname = $str.$v -> tname;
        // }
        // dd($request -> all());
        return view('admin.category.index',['title' => '分类列表', 'data' => $data,'request' => $request -> all()]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //

        // $data = DB::table('t_class') -> get();
        $data = DB::table('t_class') -> select('*',DB::raw("concat(path,',',id) as path_str")) -> orderBy('path_str') -> get();
        // dd($data);

        foreach ($data as $k => $v) {
			$num = substr_count($v -> path, ',');
			$str = str_repeat('----|', $num);
			$data[$k] -> tname = $str.$v -> tname;
	    }
        // dd($data);
        return view('admin.category.addPlate',['title' => '分类管理', 'data' => $data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($request -> all());
        $data = $request -> only('tname', 'pid');
        // dd($data);
        //判断是否为添加根分类
        if($request -> pid == 0){
            $data['path'] = 0;

        }else
        {
            $parent_path = DB::table('t_class') -> where('id', $request -> pid) -> first() -> path;
            $data['path']= $parent_path.','.$request -> pid;
        }

        //处理分区图片
            if($request -> hasFile('picture'))
            {
                if($request -> file('picture') -> isValid())
                {
                    $suffix = $request -> file('picture') -> 
                        getClientOriginalExtension();
                    $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                    //移动文件
                    $move = $request -> file('picture') -> move('./uploads/category', $fileName);
                }
                    if($move)
                    {
                        $data['picture'] = $fileName;
                    }else
                    {
                        $data['picture'] = 'default.jpg';
                    }
            }else
            {
                $data['picture'] = 'default.jpg';
            }
        // dd($data);
        //添加
        $res = DB::table('t_class') -> insert($data);
        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '添加成功']);
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = DB::table('t_class') -> where('id', $id) -> get();
        return view('admin.category.edit',['title' => '分区编辑', 'data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request -> except('_token');   
        // return $id;
        //获取原来图片
        $oldPic = DB::table('t_class') -> where('id', $id) -> first() -> picture;
        // dd($oldPic);
        if($request -> hasFile('picture'))
        {
            if($request -> file('picture') -> isValid())
            {
                $suffix = $request -> file('picture') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                //移动文件
                $move = $request -> file('picture') -> move('./uploads/category',$fileName);

                if($move)
                {
                    $data['picture'] = $fileName;
                    //删除原来图片
                    unlink('./uploads/category/'.$oldPic);
                    
                }
            }
        }
        $res = DB::table('t_class') -> where('id', $id) -> update($data);
        if($res)
        {
            return redirect('/admin/category');
        }else
        {
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function ajaxStatus(Request $request)
    {
        // dd($request -> all()['id']);
        $id = $request -> all()['id'];
        $status = DB::table('t_class') -> where('id', $id) -> value('status');
        if($status == 1)
        {
            $data = DB::table('t_class') -> where('id', $id) -> update(['status' => 0]);
            if($data)
            {
                return 1;
            }else
            {
                return 2;
            }
        }elseif($status == 0)
        {
            $data = DB::table('t_class') -> where('id', $id) -> update(['status' => 1]);
            if($data)
            {
                return 1;
            }else
            {
                return 2;
            }
        }
    }
}
